﻿using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Folding;
using ICSharpCode.AvalonEdit.Highlighting;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Xml;

namespace NB_Controller_Test
{
    /// <summary>
    /// ScnControlUI.xaml 的交互逻辑
    /// </summary>
    public partial class MainUI : UserControl
    {
        private UdpDataSndRecv udpDataRecv;

        private NB_ContentTypeManager typeManager = new NB_ContentTypeManager();

        private FoldingManager foldingManager_tbSndMsgData = null;
        private FoldingManager foldingManager_tbSndMsgData1 = null;
        private FoldingManager foldingManager_tbRecvMsgData = null;
        private FoldingManager foldingManager_tbSndReMsgData = null;
        private AbstractFoldingStrategy foldingStrategy_tbSndMsgData = new BraceFoldingStrategy();
        private AbstractFoldingStrategy foldingStrategy_tbSndMsgData1 = new BraceFoldingStrategy();
        private AbstractFoldingStrategy foldingStrategy_tbRecvMsgData = new BraceFoldingStrategy();
        private AbstractFoldingStrategy foldingStrategy_tbSndReMsgData = new BraceFoldingStrategy();

        private DispatcherTimer foldingUpdateTimer;
        public MainUI()
        {
            InitializeComponent();

            typeManager.LoadXmlConfig("Config.xml");
            cbContentType.Items.Clear();
            foreach (ContentType type in typeManager.contentTypeList)
            {
                ComboBoxItem itemType = new ComboBoxItem();
                itemType.Content = type.StrTypeName;

                cbContentType.Items.Add(itemType);
            }
            if (cbContentType.Items.Count > 0)
            {
                cbContentType.SelectedIndex = 0;
            }

            //srcIP
            ComboBoxItem itemIP = new ComboBoxItem();
            //itemIP.Content = "0.0.0.0";
            //srcIP.Items.Add(itemIP);

            List<string> strIPs = GlobalFun.GetIPAddress();
            foreach (string strIP in strIPs)
            {
                itemIP = new ComboBoxItem();
                itemIP.Content = strIP;
                srcIP.Items.Add(itemIP);
            }
            srcIP.SelectedIndex = 0;

            //devIP
            itemIP = new ComboBoxItem();
            itemIP.Content = "0.0.0.0";
            itemIP.Tag = IntPtr.Zero;
            devIP.Items.Add(itemIP);

            devIP.SelectedIndex = 0;

            udpDataRecv = UdpDataSndRecv.GetUdpDataRecv();
            udpDataRecv.RegisterMsgProc(HandleUdpMsg);

            //HightLight
            //以下设置，若被包含在父控件中，设置无法马上奇效果，原因不明.
            //tbSndMsgDataCbHightLight.Text = "JavaScript";
            //tbRecvMsgDataCbHightLight.Text = "C#";
            //tbSndReMsgDataCbHightLight.Text = "C++";


            //自动更新显示折叠大纲定时器
            foldingUpdateTimer = new DispatcherTimer();
            foldingUpdateTimer.Interval = TimeSpan.FromSeconds(1);
            foldingUpdateTimer.Tick += foldingUpdateTimer_Tick;
            foldingUpdateTimer.Start();
        }

        //private bool bFirstTime = true;
        private void foldingUpdateTimer_Tick(object sender, EventArgs se)
        {
            //if (bFirstTime)
            {
                tbSndMsgDataCbHightLight.Text = "JavaScript";
                tbSndMsgDataCbHightLight1.Text = "JavaScript";
                tbRecvMsgDataCbHightLight.Text = "C#";
                tbSndReMsgDataCbHightLight.Text = "C++";
                //bFirstTime = false;
            }

            if (foldingStrategy_tbSndMsgData != null)
            {
                foldingStrategy_tbSndMsgData.UpdateFoldings(foldingManager_tbSndMsgData, tbSndMsgData.Document);
            }

            if (foldingStrategy_tbSndMsgData1 != null)
            {
                foldingStrategy_tbSndMsgData1.UpdateFoldings(foldingManager_tbSndMsgData1, tbSndMsgData1.Document);
            }

            if (foldingStrategy_tbRecvMsgData != null)
            {
                foldingStrategy_tbRecvMsgData.UpdateFoldings(foldingManager_tbRecvMsgData, tbRecvMsgData.Document);
            }

            if (foldingStrategy_tbSndReMsgData != null)
            {
                foldingStrategy_tbSndReMsgData.UpdateFoldings(foldingManager_tbSndReMsgData, tbSndReMsgData.Document);
            }

            foldingUpdateTimer.Stop();
        }

        public void HandleUdpMsg(object obj, Byte byType)
        {
            RecvData recvData = obj as RecvData;

            TextEditor tb = tbRecvMsgData;
            if (byType == 1)
                tb = tbSndReMsgData;
            //else
            //    AddConnIDToDevCombox(recvData.connId);

            //超过一万行自动清空，防止界面卡顿
            if (tb.LineCount > 1 * 10000)
                tb.Text = "";

            //根据协议格式除了中间Json为ASCII外其他格式化为16进制数据
            string strData = GlobalFun.ByteArrayToString(recvData.byData, Encoding.ASCII);
            string strBinaryData = GlobalFun.ByteArrayToHexString(recvData.byData, recvData.byData.Length, " ");
            string strTime = DateTime.Now.ToString("HH:mm:ss fff ");
            System.Diagnostics.Debug.Write(strTime + " [Recv] \r\n [Binary] " + strBinaryData);


            //IP Port解析
            string strIP = "";
            UInt16 uiPort = 0;
            NetLink.GetNetLink().udpServerController.GetIPAndPortByConnId(recvData.connId, ref strIP, ref uiPort);

            //这里假定第一个{出现的序号后面的都是JSon 数据，其实是不合理的。
            int nJsonStartInx = strData.IndexOf("{");
            if (nJsonStartInx > 0)
            {
                int nHeadLen = Marshal.SizeOf(NB_Protocol.NB_PROT_HEAD_FLAG);
                int nHeadProlLen = (1 + 1 + 2 + 2) + (4 + 1 + 1);
                int nTailLen = Marshal.SizeOf(NB_Protocol.NB_PROT_TAIL_FLAG);
                Byte[] byHeadData = new Byte[nHeadLen];
                Array.Copy(recvData.byData, 0, byHeadData, 0, nHeadLen);

                //下面的大小与NB_Protocol协议定义存在关联，后续修改要注意
                Byte[] byHeadProlData = new Byte[nHeadProlLen];
                Array.Copy(recvData.byData, nHeadLen, byHeadProlData, 0, nHeadProlLen);


                Byte[] byJsonData = new Byte[recvData.byData.Length - nHeadLen - nHeadProlLen - nTailLen];
                Array.Copy(recvData.byData, nHeadLen + nHeadProlLen, byJsonData, 0, byJsonData.Length);

                Byte[] byEndData = new Byte[nTailLen];
                Array.Copy(recvData.byData, recvData.byData.Length - nTailLen, byEndData, 0, nTailLen);

                string strHeadData = GlobalFun.ByteArrayToHexString(byHeadData, byHeadData.Length, " ") + "\r\n";
                string strHeadProlData = GlobalFun.ByteArrayToHexString(byHeadProlData, byHeadProlData.Length, " ") + "\r\n";
                string strJsonData = GlobalFun.ByteArrayToString(byJsonData, Encoding.ASCII);
                string strEndData = GlobalFun.ByteArrayToHexString(byEndData, byEndData.Length, " ") + "\r\n";

                try
                {
                    Newtonsoft.Json.Linq.JObject jObj = Newtonsoft.Json.Linq.JObject.Parse(strJsonData);
                    //除去多余无效\r\n以及空格
                    strJsonData = jObj.ToString();

                    JToken jtProp = jObj.GetValue("prop");
                    if (jtProp != null)
                    {
                        string strProp = jtProp.ToString();
                        int nIndex = strProp.IndexOf("imei");
                        int nEndIndex = strProp.IndexOf("imsi");
                        if (nEndIndex > nIndex)
                        {
                            string strNBBase = strProp.Substring(nIndex + 6, nEndIndex - nIndex - 6 - 8);

                            if (byType != 1)
                                AddConnIDToDevCombox(recvData.connId, " [imei:" + strNBBase + "]");
                        }
                    }
                }
                catch (Newtonsoft.Json.JsonReaderException e)
                {
                    e = e;
                }

                tb.Text += ("//" + strTime + " [" + strIP + " : " + uiPort.ToString() + "] \r\n" + strHeadData + strHeadProlData + strJsonData + "\r\n" + strEndData) + "\r\n";

                //System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss fff ") + " [Show UI] " + tb.Text);
                System.Diagnostics.Debug.Write("\r\n [Format] " + strHeadData + strJsonData + strEndData);
            }
            else
            {
                tb.Text += ("//" + strTime + " [" + strIP + " : " + uiPort.ToString() + "] \r\n" + strData) + "\r\n";
                //System.Diagnostics.Debug.WriteLine(strTime + " [Recv] " + strData);
            }

            System.Diagnostics.Debug.Write("\r\n");

            //自动滚动到底部
            //((ScrollViewer)(tb.Parent)).ScrollToEnd();
            tb.ScrollToEnd();

            if (foldingStrategy_tbSndMsgData != null && foldingManager_tbSndMsgData != null)
            {
                foldingStrategy_tbSndMsgData.UpdateFoldings(foldingManager_tbSndMsgData, tbSndMsgData.Document);
            }

            if (foldingStrategy_tbSndMsgData1 != null && foldingManager_tbSndMsgData1 != null)
            {
                foldingStrategy_tbSndMsgData1.UpdateFoldings(foldingManager_tbSndMsgData1, tbSndMsgData1.Document);
            }

            if (foldingStrategy_tbRecvMsgData != null && foldingManager_tbRecvMsgData != null)
            {
                foldingStrategy_tbRecvMsgData.UpdateFoldings(foldingManager_tbRecvMsgData, tbRecvMsgData.Document);
            }

            if (foldingStrategy_tbSndReMsgData != null && foldingManager_tbSndReMsgData != null)
            {
                foldingStrategy_tbSndReMsgData.UpdateFoldings(foldingManager_tbSndReMsgData, tbSndReMsgData.Document);
            }
        }

        private DispatcherTimer reTimesTimer;
        private UInt16 uiTimes = 0;
        private class TempSendMsg
        {
            public byte[] data;
            public int dataLen;
            public IntPtr connId;
        }

        private UInt32 _ulSeqNum = 0x00001234;
        private void ButSendMsg(object sender, System.Windows.RoutedEventArgs e)
        {
            NB_Protocol nbProl = new NB_Protocol();
            nbProl.byProlID = GlobalFun.HexStringToByte(tbProlID.Text);
            nbProl.byProlVer = GlobalFun.HexStringToByte(tbProlVer.Text);

            nbProl.nbProlPayload.ulSeqNum = _ulSeqNum++;
            nbProl.nbProlPayload.byIsRsp = GlobalFun.HexStringToByte(tbIsRsp.Text);
            nbProl.nbProlPayload.byContentType = GlobalFun.HexStringToByte(cbContentType.Text);
            nbProl.nbProlPayload.strContentData = tbSndMsgData.Text;

            Byte[] byMsgData = SendMsg(nbProl);

            //判断tbSndMsgData1是否为空，若不是，则尝试发送
            Byte[] byMsgData1 = null;
            if (tbSndMsgData1.Text.Length > 0)
            {
                nbProl = new NB_Protocol();
                nbProl.byProlID = GlobalFun.HexStringToByte(tbProlID.Text);
                nbProl.byProlVer = GlobalFun.HexStringToByte(tbProlVer.Text);

                nbProl.nbProlPayload.ulSeqNum = _ulSeqNum++;
                nbProl.nbProlPayload.byIsRsp = GlobalFun.HexStringToByte(tbIsRsp.Text);
                nbProl.nbProlPayload.byContentType = GlobalFun.HexStringToByte(cbContentType.Text);
                nbProl.nbProlPayload.strContentData = tbSndMsgData1.Text;

                byMsgData1 = SendMsg(nbProl);
            }

            //return;

            if (cbReEnable.IsChecked == true && reTimesTimer == null)
            {
                //获取重发次数和间隔时间
                UInt16 uiReTimes = GlobalFun.StringToUInt16(tbReTimes.Text);
                UInt16 uiReTime = GlobalFun.StringToUInt16(tbReTime.Text);

                //自动更新显示折叠大纲定时器
                if (reTimesTimer != null)
                {
                    reTimesTimer.Stop();
                }

                reTimesTimer = new DispatcherTimer();
                reTimesTimer.Interval = TimeSpan.FromMilliseconds(uiReTime);
                reTimesTimer.Tick += reTimesTimer_Tick;

                /*
                //判断当前选中的devIP
                IntPtr intPrt = IntPtr.Zero;
                ComboBoxItem itemSel = devIP.SelectedItem as ComboBoxItem;
                if (itemSel != null)
                {
                    intPrt = (IntPtr)(itemSel.Tag);
                }


                //0
                List<TempSendMsg> sndMsgList = new List<TempSendMsg>();
                TempSendMsg sndMsg = new TempSendMsg();
                sndMsg.dataLen = byMsgData.Length;                
                sndMsg.connId = intPrt;
                sndMsg.data = new Byte[sndMsg.dataLen];
                Array.Copy(byMsgData, sndMsg.data, byMsgData.Length);
                sndMsgList.Add(sndMsg);

                //1
                TempSendMsg sndMsg1 = new TempSendMsg();
                sndMsg1.dataLen = byMsgData1.Length;                
                sndMsg1.connId = intPrt;
                sndMsg1.data = new Byte[sndMsg1.dataLen];
                Array.Copy(byMsgData1, sndMsg1.data, byMsgData1.Length);
                sndMsgList.Add(sndMsg1);

                reTimesTimer.Tag = sndMsgList;
                 * */

                butSend.IsEnabled = false;

                uiTimes = uiReTimes;
                lbTimes.Content = uiTimes.ToString() + " / " + tbReTimes.Text;

                reTimesTimer.Start();
            }
        }

        private Byte[] SendMsg(NB_Protocol nbProl)
        {
            Byte[] byMsgData = nbProl.GetProlMsgData();
            if (byMsgData == null)
            {
                System.Diagnostics.Debug.WriteLine("byMsgData == null");
                return null;
            }
            if (byMsgData.Length > 0x05c0)
            {
                System.Diagnostics.Debug.WriteLine("byMsgData.Length > 0x05c0");
                System.Windows.MessageBox.Show("UDP包数据量太大，超过了0x05c0大小限制！");
                return null;
            }

            //判断当前选中的devIP
            IntPtr intPrt = IntPtr.Zero;
            ComboBoxItem itemSel = devIP.SelectedItem as ComboBoxItem;
            if (itemSel != null)
            {
                intPrt = (IntPtr)(itemSel.Tag);
            }

            udpDataRecv.SendMsgData(byMsgData, byMsgData.Length, intPrt);

            return byMsgData;
        }

        private void ButScan(object sender, System.Windows.RoutedEventArgs e)
        {
            NB_Protocol nbProl = new NB_Protocol();
            nbProl.byProlID = GlobalFun.HexStringToByte(tbProlID.Text);
            nbProl.byProlVer = GlobalFun.HexStringToByte(tbProlVer.Text);

            nbProl.nbProlPayload.ulSeqNum = _ulSeqNum++;
            nbProl.nbProlPayload.byIsRsp = GlobalFun.HexStringToByte(tbIsRsp.Text);
            nbProl.nbProlPayload.byContentType = GlobalFun.HexStringToByte(cbContentType.Text);
            string strScan = "{\"reqId\": 40,\"op\": \"R\",\"prop\": [\"nbBase\"]}";
            nbProl.nbProlPayload.strContentData = strScan;

            Byte[] byMsgData = SendMsg(nbProl);
        }

        private void reTimesTimer_Tick(object sender, EventArgs e)
        {
            uiTimes -= 1;
            lbTimes.Content = uiTimes.ToString() + " / " + tbReTimes.Text;

            if (uiTimes == 0)
            {
                reTimesTimer.Stop();
                reTimesTimer = null;
                butSend.IsEnabled = true;
                lbTimes.Content = "发送完毕！";
            }
            else
            {
                ButSendMsg(null, null);
                return;
                List<TempSendMsg> sndMsgList = reTimesTimer.Tag as List<TempSendMsg>;
                foreach (TempSendMsg sndMsg in sndMsgList)
                {
                    if (sndMsg != null)
                    {
                        udpDataRecv.SendMsgData(sndMsg.data, sndMsg.dataLen, sndMsg.connId);
                    }
                }
            }
        }

        private void cbContentType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            ComboBoxItem item = cb.SelectedItem as ComboBoxItem;

            string strText = string.Empty;
            if (item != null)
            {
                strText = item.Content.ToString();
            }
            //更新当前
            ContentType contType = new ContentType();
            contType.StrProlID = tbProlID.Text;
            contType.StrProlVer = tbProlVer.Text;
            contType.StrIsRsp = tbIsRsp.Text;
            contType.StrTypeJsonBody = tbSndMsgData.Text;
            contType.StrTypeJsonBody1 = tbSndMsgData1.Text;
            typeManager.UpdateByName(cb.Text, contType);

            //反查
            tbSndMsgData.Text = "";
            tbSndMsgData1.Text = "";
            contType = typeManager.FindJsonBodyByName(strText);
            if (contType != null)
            {
                tbProlID.Text = contType.StrProlID;
                tbProlVer.Text = contType.StrProlVer;
                tbIsRsp.Text = contType.StrIsRsp;
                tbSndMsgData.Text = contType.StrTypeJsonBody;
                tbSndMsgData1.Text = contType.StrTypeJsonBody1;
            }

        }

        private void ButSetIP(object sender, System.Windows.RoutedEventArgs e)
        {
            NetLink.GetNetLink().UnInitNetLink();
            NetLink.GetNetLink().InitNetLink(srcIP.Text);
            System.Windows.Forms.MessageBox.Show("绑定成功！");
        }

        private void AddConnIDToDevCombox(IntPtr connID, string strIMEIInfo)
        {
            //先确认要添加的ConnID是否已添加过
            foreach (ComboBoxItem item in devIP.Items)
            {
                if ((IntPtr)(item.Tag) == connID)
                    return;
            }

            //IP Port解析
            string strIP = "";
            UInt16 uiPort = 0;
            NetLink.GetNetLink().udpServerController.GetIPAndPortByConnId(connID, ref strIP, ref uiPort);

            ComboBoxItem itemIP = new ComboBoxItem();
            itemIP.Content = strIP + strIMEIInfo;
            itemIP.Tag = connID;
            devIP.Items.Add(itemIP);
        }

        private void ButClrRecvMsg(object sender, System.Windows.RoutedEventArgs e)
        {
            if (System.Windows.Forms.MessageBox.Show("确定要清空？", "警告", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                tbRecvMsgData.Text = "";
        }

        private void ButClrSendMsg(object sender, System.Windows.RoutedEventArgs e)
        {
            if (System.Windows.Forms.MessageBox.Show("确定要清空？", "警告", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                tbSndReMsgData.Text = "";
        }

        private void tbSndMsgDataHighlightCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (tbSndMsgData.SyntaxHighlighting == null)
            {
                foldingStrategy_tbSndMsgData = null;
            }
            else
            {
                switch (tbSndMsgData.SyntaxHighlighting.Name)
                {
                    case "XML":
                        foldingStrategy_tbSndMsgData = new XmlFoldingStrategy();
                        break;
                    case "C#":
                    case "C++":
                    case "PHP":
                    case "Java":
                        foldingStrategy_tbSndMsgData = new BraceFoldingStrategy();
                        break;
                    default:
                        foldingStrategy_tbSndMsgData = null;
                        break;
                }
            }
            if (foldingStrategy_tbSndMsgData != null)
            {
                if (foldingManager_tbSndMsgData == null)
                {
                    foldingManager_tbSndMsgData = FoldingManager.Install(tbSndMsgData.TextArea);
                }
                foldingStrategy_tbSndMsgData.UpdateFoldings(foldingManager_tbSndMsgData, tbSndMsgData.Document);
            }
            else
            {
                if (foldingManager_tbSndMsgData != null)
                {
                    FoldingManager.Uninstall(foldingManager_tbSndMsgData);
                    foldingManager_tbSndMsgData = null;
                }
            }
        }

        private void tbSndMsgDataHighlightCB_SelectionChanged1(object sender, SelectionChangedEventArgs e)
        {
            if (tbSndMsgData1.SyntaxHighlighting == null)
            {
                foldingStrategy_tbSndMsgData1 = null;
            }
            else
            {
                switch (tbSndMsgData1.SyntaxHighlighting.Name)
                {
                    case "XML":
                        foldingStrategy_tbSndMsgData1 = new XmlFoldingStrategy();
                        break;
                    case "C#":
                    case "C++":
                    case "PHP":
                    case "Java":
                        foldingStrategy_tbSndMsgData1 = new BraceFoldingStrategy();
                        break;
                    default:
                        foldingStrategy_tbSndMsgData1 = null;
                        break;
                }
            }
            if (foldingStrategy_tbSndMsgData1 != null)
            {
                if (foldingManager_tbSndMsgData1 == null)
                {
                    foldingManager_tbSndMsgData1 = FoldingManager.Install(tbSndMsgData1.TextArea);
                }
                foldingStrategy_tbSndMsgData1.UpdateFoldings(foldingManager_tbSndMsgData1, tbSndMsgData1.Document);
            }
            else
            {
                if (foldingManager_tbSndMsgData1 != null)
                {
                    FoldingManager.Uninstall(foldingManager_tbSndMsgData1);
                    foldingManager_tbSndMsgData1 = null;
                }
            }
        }

        private void tbRecvMsgDataHighlightCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (tbRecvMsgData.SyntaxHighlighting == null)
            {
                foldingStrategy_tbRecvMsgData = null;
            }
            else
            {
                switch (tbRecvMsgData.SyntaxHighlighting.Name)
                {
                    case "XML":
                        foldingStrategy_tbRecvMsgData = new XmlFoldingStrategy();
                        break;
                    case "C#":
                    case "C++":
                    case "PHP":
                    case "Java":
                        foldingStrategy_tbRecvMsgData = new BraceFoldingStrategy();
                        break;
                    default:
                        foldingStrategy_tbRecvMsgData = null;
                        break;
                }
            }
            if (foldingStrategy_tbRecvMsgData != null)
            {
                if (foldingManager_tbRecvMsgData == null)
                {
                    foldingManager_tbRecvMsgData = FoldingManager.Install(tbRecvMsgData.TextArea);
                }

                foldingStrategy_tbRecvMsgData.UpdateFoldings(foldingManager_tbRecvMsgData, tbRecvMsgData.Document);
            }
            else
            {
                if (foldingManager_tbRecvMsgData != null)
                {
                    FoldingManager.Uninstall(foldingManager_tbRecvMsgData);
                    foldingManager_tbRecvMsgData = null;
                }
            }
        }

        private void tbSndReMsgDataHighlightCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (tbSndReMsgData.SyntaxHighlighting == null)
            {
                foldingStrategy_tbSndReMsgData = null;
            }
            else
            {
                switch (tbSndReMsgData.SyntaxHighlighting.Name)
                {
                    case "XML":
                        foldingStrategy_tbSndReMsgData = new XmlFoldingStrategy();
                        break;
                    case "C#":
                    case "C++":
                    case "PHP":
                    case "Java":
                        foldingStrategy_tbSndReMsgData = new BraceFoldingStrategy();
                        break;
                    default:
                        foldingStrategy_tbSndReMsgData = null;
                        break;
                }
            }
            if (foldingStrategy_tbSndReMsgData != null)
            {
                if (foldingManager_tbSndReMsgData == null)
                {
                    foldingManager_tbSndReMsgData = FoldingManager.Install(tbSndReMsgData.TextArea);
                }

                foldingStrategy_tbSndReMsgData.UpdateFoldings(foldingManager_tbSndReMsgData, tbSndReMsgData.Document);
            }
            else
            {
                if (foldingManager_tbSndReMsgData != null)
                {
                    FoldingManager.Uninstall(foldingManager_tbSndReMsgData);
                    foldingManager_tbSndReMsgData = null;
                }
            }
        }
    }
}
